#pragma once

#include "helpers/general.h"
#include <hitrace/trace.h>

namespace ohos_rive {
/**
 * Interface for a generic Tracer
 */
class ITracer {
public:
    ITracer() = default;
    virtual ~ITracer() = default;
    virtual void beginSection(const char *sectionName) = 0;
    virtual void endSection() = 0;
};

class NoopTracer : public ITracer {
public:
    NoopTracer() = default;
    ~NoopTracer() override = default;
    void beginSection(const char *sectionName) override {};
    void endSection() override {};
};

class Tracer : public ITracer {
public:
    Tracer() : traceId({}) {}
    ~Tracer() override = default;

    void beginSection(const char *sectionName) override
    {
        // 使用OHOS HiTraceChain API开始跟踪
        if (sectionName != nullptr) {
            traceId = OH_HiTrace_BeginChain(sectionName, HITRACE_FLAG_DEFAULT);
        }
    };

    void endSection() override
    {
        // 使用OHOS HiTraceChain API结束跟踪
        OH_HiTrace_EndChain();
        traceId = {};
    };

private:
    HiTraceId traceId; // HiTraceId的初始化由OH_HiTrace_Begin完成
};
} // namespace ohos_rive